# 导入模块
import sys
# 窗口配置
from PyQt5.QtWidgets import *
# 图像相关
from PyQt5.QtGui import *
# QtCore相关
from PyQt5.QtCore import *

from ui.Ui_MainWindow import Ui_MainWindow
from views.ConnectDialog import MyDialog
from views.Environment import MyEnvironmentWidget
from views.Furniture import MyHomeFunctionWidget
import threading

class MyMainWindow(QMainWindow):   
    
    def __init__(self, parent = None):
        super().__init__(parent)

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.myDialog = MyDialog()
        self.myEnvironmentWidget = MyEnvironmentWidget()
        self.myHomeFunctionWidget = MyHomeFunctionWidget()

        self.init_ui()

    def init_ui(self):
        self.ui.tabWidget.addTab(self.myEnvironmentWidget, "家居环境")
        self.ui.tabWidget.addTab(self.myHomeFunctionWidget, "家居功能")
        self.ui.connectAction.triggered.connect(self.connectBluetooth) # 连接蓝牙
        self.ui.dicconnectAction.triggered.connect(self.disconnectBluetooth) # 断开蓝牙
        self.myDialog.dataHandle.temShowSignal.connect(self.myEnvironmentWidget.showTemperature) # 显示温度
        self.myDialog.dataHandle.humSHowSignal.connect(self.myEnvironmentWidget.showHumidness) # 显示湿度
        self.myDialog.dataHandle.lightShowSignal.connect(self.myEnvironmentWidget.showLight) # 显示光照强度
        self.myDialog.dataHandle.smokeShowSignal.connect(self.myEnvironmentWidget.showSmoke) # 显示烟雾浓度
        self.myDialog.dataHandle.rainShowSignal.connect(self.myEnvironmentWidget.showRain) # 显示雨量
        self.myHomeFunctionWidget.sendAutoSignal.connect(self.myDialog.sendData) # 发送自动化指令
        self.myHomeFunctionWidget.sendDoorSignal.connect(self.myDialog.sendData) # 发送门密码指令
        self.myHomeFunctionWidget.sendDataSignal.connect(self.myDialog.sendData) # 发送其他指令

    def connectBluetooth(self):
        self.myDialog.exec_()
        
    def disconnectBluetooth(self):
        self.myDialog.disconnnectBluetooth()
    

if __name__ == '__main__':
    # 创建应用程序
    app = QApplication(sys.argv)

    # 创建窗口
    w = MyMainWindow()

    # 显示窗口
    w.show()

    # 等待程序结束
    sys.exit(app.exec_())